package com.hqw.mindanews.adapter;

import java.util.List;

import android.R.string;
import android.content.Context;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.opengl.Visibility;
import android.support.v7.widget.LinearLayoutCompat.LayoutParams;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import cn.bmob.v3.datatype.BmobFile;
import cn.pedant.SweetAlert.SweetAlertDialog;

import com.android.volley.toolbox.ImageLoader;
import com.android.volley.toolbox.ImageLoader.ImageContainer;
import com.android.volley.toolbox.ImageLoader.ImageListener;
import com.bmob.BmobProFile;
import com.hqw.mindanews.R;
import com.hqw.mindanews.R.color;
import com.hqw.mindanews.bean.NewsMsg;
import com.hqw.mindanews.log.Log;
import com.hqw.mindanews.utils.Constant;
import com.hqw.mindanews.utils.DisplayUtil;
import com.hqw.mindanews.utils.LruImageCache;
import com.hqw.mindanews.utils.PhotoUtil;
import com.hqw.mindanews.utils.StringUtil;
import com.hqw.mindanews.utils.VolleyUtil;

public class MyNewsAdapter extends BaseAdapter {

	private List<NewsMsg> mList;
	private LayoutInflater mInflater;
	private ImageLoader imageLoader;
	private Context mContext;

	public MyNewsAdapter(Context context, List<NewsMsg> data) {
		mList = data;
		mContext = context;
		this.imageLoader = new ImageLoader(VolleyUtil.getQueue(context),
				new LruImageCache());
		mInflater = LayoutInflater.from(context);

	}

	@Override
	public int getCount() {

		return mList.size();
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return mList.get(position);
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

//	public static void setMargins(View v, int l, int t, int r, int b) {
//		if (v.getLayoutParams() instanceof ViewGroup.MarginLayoutParams) {
//			ViewGroup.MarginLayoutParams p = (ViewGroup.MarginLayoutParams) v
//					.getLayoutParams();
//			p.setMargins(l, t, r, b);
//			v.requestLayout();
//		}
//	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		MyNewsViewHolder viewHolder = null;
		int imageLength = mList.get(position).getBmobFiles().size();
		if (convertView == null) {
			viewHolder = new MyNewsViewHolder();
			convertView = mInflater.inflate(R.layout.item_my_news_list, null);
			viewHolder.msgContent = (TextView) convertView.findViewById(R.id.mynews_content);
			viewHolder.mType = (TextView) convertView.findViewById(R.id.is_pass);
			viewHolder.imageview0 = (ImageView) convertView.findViewById(R.id.frist_image);
			convertView.setTag(viewHolder);

		} else {
			viewHolder = (MyNewsViewHolder) convertView.getTag();
		}
		
		if (imageLength<=0) {
			viewHolder.imageview0.setVisibility(View.GONE);
		}else {
			mList.get(position).getBmobFiles().get(0).loadImage(mContext, viewHolder.imageview0);
		}
		viewHolder.msgContent.setText(mList.get(position).getContent());
		int type = mList.get(position).getType();
		switch (type) { // 审核类型 0 正在审核， 1，审核通过 2，审核不通过，
		case 0:
			viewHolder.mType.setText("正在审核");
			viewHolder.mType.setTextColor(Color.GRAY);
			break;
		case 1:
			viewHolder.mType.setText("审核通过");
			viewHolder.mType.setTextColor(Color.GREEN);
			break;
		case 2:
			viewHolder.mType.setTextColor(Color.RED);
			viewHolder.mType.setText("审核不通过");
			break;

		default:
			break;
		}

		return convertView;
	}

//	void setImage(ImageView imageView, String imageUrl) {
//
//		ImageContainer container;
//		
//		try {
//
//			if (imageView.getTag() != null) {
//				container = (ImageContainer) imageView.getTag();
//				container.cancelRequest();
//			}else {
//				
//			}
//		} catch (Exception e) {
//
//		}
//
//		ImageListener listener = ImageLoader.getImageListener(imageView,
//				R.drawable.bg_transparent, R.drawable.bg_transparent);
//		
//		container = imageLoader.get(StringUtil.preUrl(imageUrl), listener);
//		
//		imageView.setTag(container);
//
//	}

	class MyNewsViewHolder {
		public TextView msgContent;
		public TextView mType;
		public ImageView imageview0 = null;
	}

}
